/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhu.profiteer.data;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import jhu.profiteer.data.exceptions.NonexistentEntityException;
import jhu.profiteer.data.exceptions.PreexistingEntityException;

/**
 *
 * @author Jeremy
 */
public class AirplaneCargoJpaController {

  public AirplaneCargoJpaController() {
    emf = Persistence.createEntityManagerFactory("profiteer-gamePU");
  }
  private EntityManagerFactory emf = null;

  public EntityManager getEntityManager() {
    return emf.createEntityManager();
  }

  public void create(AirplaneCargo airplaneCargo)
          throws PreexistingEntityException, Exception {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      em.persist(airplaneCargo);
      em.getTransaction().commit();
    } catch (Exception ex) {
      if(findAirplaneCargo(airplaneCargo.getId().getAirplaneId(),
              airplaneCargo.getId().getCommodityId()) != null) {
        throw new PreexistingEntityException("AirplaneCargo " + airplaneCargo +
                " already exists.", ex);
      }
      throw ex;
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public void edit(AirplaneCargo airplaneCargo)
          throws NonexistentEntityException, Exception {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      airplaneCargo = em.merge(airplaneCargo);
      em.getTransaction().commit();
    } catch (Exception ex) {
      String msg = ex.getLocalizedMessage();
      if (msg == null || msg.length() == 0) {
        Long id = airplaneCargo.getId().getAirplaneId();
        if (findAirplaneCargo(airplaneCargo.getId().getAirplaneId(),
                airplaneCargo.getId().getCommodityId()) == null) {
          throw new NonexistentEntityException("The airplaneCargo with id " +
                  id + " no longer exists.");
        }
      }
      throw ex;
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public void destroy(Long airplaneId, Long commodityId) {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      Query q = em.createQuery("delete from AirplaneCargo as o" +
              " where o.id.airplaneId = :airplaneId" +
              " and o.id.commodityId = :commodityId")
              .setParameter("airplaneId", airplaneId)
              .setParameter("commodityId", commodityId);
      q.executeUpdate();
      em.getTransaction().commit();
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public void destroyWithAirplaneId(Long airplaneId) {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      Query q = em.createQuery("delete from AirplaneCargo as o" +
              " where o.id.airplaneId = :airplaneId")
              .setParameter("airplaneId", airplaneId);
      q.executeUpdate();
      em.getTransaction().commit();
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public void destroyWithCommodityId(Long commodityId) {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      Query q = em.createQuery("delete from AirplaneCargo as o" +
              " where o.id.commodityId = :commodityId")
              .setParameter("commodityId", commodityId);
      q.executeUpdate();
      em.getTransaction().commit();
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public List<AirplaneCargo> findAirplaneCargoEntities() {
    EntityManager em = getEntityManager();
    try {
      return em.createQuery("select object(o) from AirplaneCargo as o")
              .getResultList();
    } finally {
      em.close();
    }
  }

  public List<AirplaneCargo> findAirplaneCargoEntitiesWithAirplaneId(Long airplaneId) {
    EntityManager em = getEntityManager();
    try {
      return em.createQuery("select object(o) from AirplaneCargo as o" +
              " where o.id.airplaneId = :airplaneId")
              .setParameter("airplaneId", airplaneId)
              .getResultList();
    } finally {
      em.close();
    }
  }

  public AirplaneCargo findAirplaneCargo(Long airplaneId, Long commodityId) {
    EntityManager em = getEntityManager();
    try {
      return (AirplaneCargo)em.createQuery(
              "select object(o) from AirplaneCargo as o" +
              " where o.id.airplaneId = :airplaneId" +
              " and o.id.commodityId = :commodityId")
              .setParameter("airplaneId", airplaneId)
              .setParameter("commodityId", commodityId)
              .getSingleResult();
    } finally {
      em.close();
    }
  }

  public boolean testForAirplaneCargo(Long airplaneId, Long commodityId)
  {
    EntityManager em = getEntityManager();
    try {
      List<Airplane> l = (List<Airplane>)em.createQuery("select object(o)" +
              " from AirplaneCargo as o where o.id.airplaneId = :airplaneId" +
              " and o.id.commodityId = :commodityId")
              .setParameter("airplaneId", airplaneId)
              .setParameter("commodityId", commodityId)
              .getResultList();
      if(1 == l.size()) return true;
      else return false;
    } finally {
      em.close();
    }
  }
}
